package com.lbd.bonus.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.bonus.bean.Bonus;
import com.lbd.bonus.service.BonusService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Yuanbo on 2016/9/22
 * 奖励类型
 */
@RestController
@RequestMapping("/bonus")
public class BonusController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    BonusService bonusService;

    /**
     * 查看奖励类型
     *
     * @param page  当前页码
     * @param rows  每页显示行数
     * @param bonus 奖励实体
     * @return 查询结果
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Bonus bonus) {
        return new Result(ResultState.SUCCESS, "查询成功", bonusService.findByPageConditionJpql(page, rows, bonus));
    }

    /**
     * 添加奖励类型
     *
     * @param bonus 要添加的奖励类型内容
     * @return 保存结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Bonus bonus) {
        bonusService.save(bonus);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 删除奖励类型
     *
     * @param ids 要删除的id数组
     * @return 删除结果
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        try {
            bonusService.del(ids);
        } catch (Exception e) {
            log.info("不允许删除已使用的奖励", e);
            return new Result(ResultState.FAIL, "不允许删除已使用的奖励", "");
        }
        return new Result(ResultState.SUCCESS, "删除成功", "");
    }

    /**
     * 跳转跟新界面
     *
     * @param id
     * @return Result
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", bonusService.findById(id));
    }

    /**
     * 修改奖励
     *
     * @param bonus 修改的奖励信息
     * @return Result
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Bonus bonus) {
        bonusService.update(bonus);
        return new Result(ResultState.SUCCESS, "更新奖励成功");
    }

}
